package com.ymttest.database.mongo;


import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

public class MongoLoginHistoryWapper {
	static MongoDBDriver mdd;

	public MongoLoginHistoryWapper() throws Exception {
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip"), 27017, "Ymt_LoginHistory");
		mdd.setPoolSize(Integer.valueOf(EnvSetup.getSystemUrl("mongoPoolSize")));
		mdd.auth("authuser", "123456");
		mdd.selectCollection("LoginHistory");
	}

	/**
	 * 改变表
	 * 
	 * @param Collection
	 *            表名
	 */
	public void changeCollection(String Collection) {
		mdd.selectCollection(Collection);
	}
	
	/**
	 * 根据Ip和创建时间删除log日志信息
	 * @param Ip 用户IP
	 * @param addTime 日志创建时间
	 * @throws Exception
	 */
	public void deleteByIp(String Ip, String addTime) throws Exception{
		mdd.delete("{'sLoginIp':'"+Ip+"','dAddTime':{$gt:{$date:'"+addTime+"'}}}");
	}
	
	/**
	 * 关闭数据库连接
	 * @throws Exception 
	 */
	public void close() throws Exception {
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set1_mongoip"),27017, "Ymt_LoginHistory");
		mdd.close();
		mdd=null;
	}
	
	/**
	 * 根据设备号和创建时间删除日志信息
	 * @param deviceId
	 * @param addTime
	 * @throws Exception 
	 */
	public void deleteByDeviceId(String deviceId, String addTime) throws Exception{
		mdd.delete("{'DeviceId':'"+deviceId+"','dAddTime':{$gt:{$date:'"+addTime+"'}}}");
	}
	
	public void deleteByLoginId(String loginId, String addTime) throws Exception{
		mdd.delete("{'sLoginId':'"+loginId+"','dAddTime':{$gt:{$date:'"+addTime+"'}}}");
	}
	
	public static void main(String[] args){
//		new LoginHistoryWapper().deleteByDeviceId(deviceId, DateUtils.getBeforeOrNextDay(1,"yyyy-MM-dd")+ "T23:00:00.439Z");
	}
}
